package edu.johnpank.libassistant.entity

import java.io.Serializable
import java.time.LocalDateTime
import java.time.ZoneOffset
import javax.persistence.*

@Entity
class Book: Serializable {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    val id: Long? = null

    @Column(nullable = false)
    var title: String = ""

    @Column(nullable = false)
    var author: String = ""

    @Column(nullable = false)
    var genre: String = ""

    @Column(nullable = false)
    var publisher: String = ""

    @Column(nullable = false)
    var isbn: String = ""

    @Column(nullable = false)
    var barcode: Long = 0L

    @Column(nullable = false)
    var year: Long = 0L

    @Column(nullable = false)
    var series: String = ""

    @Column(nullable = false)
    var city: String = ""

    @Column(nullable = false)
    var pages: Long = 0L

    @Column(nullable = false)
    var circulation: Long = 0L

    @Column(nullable = false)
    var shelfNumber: Long = 0L

    @Column(nullable = false)
    var block: Long = 0L

    @Column(nullable = false)
    var subBlock: Long = 0L

    @Column(nullable = false)
    var addDate: Long = 0L

    @Column(nullable = false)
    var comment: String = ""

    @PrePersist
    private fun init(){
        if (addDate == 0L){
            addDate = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC) * 1000
        }
    }

    override fun toString(): String {
        return "Book(id=$id, title='$title', author='$author', genre='$genre', publisher='$publisher', isbn='$isbn', barcode=$barcode, year=$year, series='$series', city='$city', pages=$pages, circulation=$circulation, shelfNumber=$shelfNumber, block=$block, subBlock=$subBlock, addDate=$addDate, comment='$comment')"
    }

}
